require('../css/mine.less')
const baseURL = require('../utils/baseURL.js')
window.onload = function () {
    common.footer('我的')
    let span = document.querySelectorAll('span')
    let img = document.querySelector('img')
    function getmsg() {
        axios.get(
            '/api/user/info'
        ).then(function (res) {
            let data = res.data.data
            img.src = baseURL + data.imgUrl
            span[0].innerHTML = data.nickName
            span[1].innerHTML = data.sign ? data.sign : '他很懒，什么都没有留下'
            span[2].innerHTML = data.duration
            span[3].innerHTML = data.calorie
        })
    }
    getmsg()

    let login = document.querySelector('.login')
    login.addEventListener('click', function () {
        common.toast('退出成功', true)
        sessionStorage.removeItem('token')
        setTimeout(function () {
            location.href = 'login.html'
        }, 2000)
    })
    let file = document.querySelector("#file")
    img.addEventListener('click', function (e) {
        file.click()
        e.preventDefault()

        file.addEventListener('change', function () {
            let formdata = new FormData()
            formdata.append('file', file.files[0])
            axios.post(
                '/api/shared/uploadPortrait1', formdata
            ).then(function (res) {
                console.log(res.data.data.url);
                let imgs = {
                    imgUrl: res.data.data.url
                }
                axios.post(
                    '/api/user/changeInfo', imgs
                ).then(function (res) {
                    console.log(res.data);
                    common.toast('修改成功', true)
                    getmsg()
                })
            })
        })
    })

    let data1 = document.querySelector('.data1>i')
    data1.addEventListener('click', function () {
        location.href = 'data.html'
    })

}